package com.bricker.laboratorymanagement.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bricker.laboratorymanagement.business.dao.SysUserMapper;
import com.bricker.laboratorymanagement.business.entity.SysUser;
import com.bricker.laboratorymanagement.business.model.ResultLogin;
import com.bricker.laboratorymanagement.business.model.UserModel;
import com.bricker.laboratorymanagement.business.model.UserSelectModel;
import com.bricker.laboratorymanagement.business.service.SysUserService;
import com.bricker.laboratorymanagement.utils.MD5Util;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 用户登录
     * @param loginUser
     * @return
     */
    @PostMapping("/login")
    public ResultLogin login(@RequestBody SysUser loginUser){
      return sysUserService.login(loginUser);
    }

    /**
     * 用户注册
     * @param registerUser
     * @return
     */
    @PostMapping("/register")
    public Map<String,Object> register(@RequestBody SysUser registerUser){
       return sysUserService.register(registerUser);
    }

    /**
     * 用户修改
     * @param updateUser
     * @return
     */
    @PostMapping("/update")
    public Map<String,Object> update(HttpServletRequest request,@RequestBody SysUser updateUser){
        //普通用户从token获取当前用户id
        //管理员用户修改普通用户则需传该用户id
        if (updateUser.getId() == null || updateUser.getId() == 0){
            String token=request.getHeader("access_token");
            Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
            updateUser.setId(uId);
        }
        return sysUserService.updateUser(updateUser);
    }


    /**
     * 分页多条件查询实验项目
     * @param userSelectModel
     * @return
     */
    @PostMapping("/list")
    public Page<UserModel> getUserByPage(@RequestBody UserSelectModel userSelectModel) {
        return sysUserService.getUserByPage(userSelectModel);
    }


    /**
     * 获取当前用户信息
     */
    @GetMapping
    public UserModel getCurrentUser(HttpServletRequest request){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        return sysUserService.getCurrentUser(uId);
    }

    @PutMapping
    public void saveOrUpdateUser(HttpServletRequest request,@RequestBody String users){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        List<SysUser> userList =JSONObject.parseArray(users,SysUser.class);
        sysUserService.saveOrUpdateUser(uId,userList);
    }

    @DeleteMapping
    public void deleteUserByIds(HttpServletRequest request,@RequestBody String ids) {
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        List<Long> idList= JSONObject.parseArray(ids,Long.class);
        SysUser user = new SysUser();
        for (Long str : idList) {
            user.setId(Long.valueOf(str));
            user.setDelFlag("1");
            user.setUpdateTime(LocalDateTime.now());
            user.setUpdateBy(uId);
            userMapper.updateById(user);
        }
    }

    @PostMapping("change/password")
    public Map<String, String> changePassword(HttpServletRequest request, @RequestBody SysUser user) {
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        Map<String, String> map = new HashMap<>();
        SysUser queryUser = userMapper.selectById(uId);
        boolean isSame = MD5Util.passwordIsTrue(user.getOldPassword(), queryUser.getPassword());
        if (isSame) {
            SysUser paramUser = new SysUser();
            paramUser.setId(uId);
            paramUser.setPassword(MD5Util.string2MD5(user.getPassword()));
            paramUser.setUpdateBy(uId);
            paramUser.setUpdateTime(LocalDateTime.now());
            userMapper.updateById(paramUser);
            map.put("code", "0");
        } else {
            map.put("code", "1");
        }
        return map;
    }

}
